package com.longxi.tianjintianbao.ui.adapter.multitype.message;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.message.Message;

import me.drakeet.multitype.ItemViewBinder;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/**
 * Created by cxw on 2018/2/20.
 */
public class MessageViewBinder extends ItemViewBinder<Message, MessageViewBinder.ViewHolder> {

    AdapterListener listener;

    public MessageViewBinder(AdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_message_no_swipe, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Message message) {
        holder.badge.setBadgeNumber(message.getUnreadMessages());//设置角标
        holder.mIvHead.setImageResource(message.getImage());
        holder.mTvName.setText(message.getName());
        holder.mTvTime.setText(message.getTime());
        if(message.getUnreadMessages() > 0){
            holder.mTvMessage.setText("点击查看新内容");
        }else{
            holder.mTvMessage.setText("暂无新消息");
        }

        holder.mRlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(holder.getAdapterPosition());
            }
        });

//        holder.mBtnDelete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                listener.onDelete(holder.getAdapterPosition());
//            }
//        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout mRlBtn;
        ImageView mIvHead;
        TextView mTvName;
        TextView mTvTime;
        TextView mTvMessage;
//        ImageView mBtnDelete;
        Badge badge;
        ViewHolder(View itemView) {
            super(itemView);
            mRlBtn = itemView.findViewById(R.id.rlBtn);
            mIvHead = itemView.findViewById(R.id.ivHead);
            mTvName = itemView.findViewById(R.id.tvName);
            mTvTime = itemView.findViewById(R.id.tvTime);
            mTvMessage = itemView.findViewById(R.id.tvMessage);
//            mBtnDelete = itemView.findViewById(R.id.btnDelete);

            badge = new QBadgeView(mIvHead.getContext()).bindTarget(mIvHead);
            badge.setBadgeGravity(Gravity.TOP | Gravity.END);
            badge.stroke(Color.WHITE, 1, true);
            badge.setShowShadow(false);
            badge.setBadgePadding(8, true);
        }
    }
}
